package com.opennews.openplatform.familyexpenseapi.controller

import com.opennews.openplatform.myspringbootcore.controller.GeneralGlobalExceptionHandler
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse
import com.opennews.openplatform.myspringbootcore.util.getErrorResponse
import org.jooq.exception.IntegrityConstraintViolationException
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.dao.DuplicateKeyException
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler

@ControllerAdvice
class GlobalExceptionHandler : GeneralGlobalExceptionHandler() {
    @ExceptionHandler(IntegrityConstraintViolationException::class)
    fun handleIntegrityConstraintViolationException(): ResponseEntity<ApiResponse<Map<String, String>>> {
        return getErrorResponse(
            HttpStatus.CONFLICT,
            mapOf("message" to "Data integrity constraint violated.")
        )
    }

    @ExceptionHandler(DataIntegrityViolationException::class)
    fun handleDataIntegrityViolationException(): ResponseEntity<ApiResponse<Map<String, String>>> {
        return getErrorResponse(
            HttpStatus.CONFLICT,
            mapOf("message" to "Data integrity constraint violated. Check for conflicting data.")
        )
    }

    @ExceptionHandler(DuplicateKeyException::class)
    fun handleDuplicateKeyException(): ResponseEntity<ApiResponse<Map<String, String>>> {
        return getErrorResponse(
            HttpStatus.CONFLICT,
            mapOf("message" to "Duplicate key error. This data already exists.")
        )
    }
}